/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.iubar.tkdscore.lwjgl;

import it.iubar.tkdscore.KeyMapping;
import it.iubar.tkdscore.TkdGlobal;
import it.iubar.tkdscore.TkdScoreApp;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.java.games.input.Component.Identifier.Key;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Keyboard;
import net.java.games.input.Rumbler;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
//import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Controllers;
import org.lwjgl.util.Display;





/**
 *
 * @author borgo
 */
public class Example1 {

    public void test7(){
ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
net.java.games.input.Controller[] cs = ce.getControllers();
PrintStream ps = getPrintStream();
int index = 0;
printDetails(cs[index], ps); // print details for the controller
ps.close();
    }

   


    public static void test_singlecontroller() {

        try {

            Controllers.create();
            int numberOfControllers = Controllers.getControllerCount();
            String playerChosenController = "MyJoystick";
            Controller playerControl = null;
            for (int i = 0; i < numberOfControllers; i++) {
                Controller c = Controllers.getController(i);
                if (c.getName().equals(playerChosenController)) {
                    playerControl = c;
                }
            }
            int buttons = playerControl.getButtonCount();
            for (int b = 0; b < buttons; b++) {
                System.out.println("Button " + b + " = " + playerControl.getButtonName(b));
            }
            int selectedIndex = 0; // the index of the button we're checking
        //    while (TimerHandler.gameLoop()) {
                playerControl.poll(); // update the controller from the hardware
                if (playerControl.isButtonPressed(selectedIndex)) {
                    // fire the huge gun!
                }
          //  }
        } catch (LWJGLException ex) {
            Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void init() {
        try {
            Controllers.create();
            System.out.println("Controllers created");
        } catch (LWJGLException ex) {
            Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
        }
        int n = Controllers.getControllerCount();
        System.out.println("Controllers number " + n);
    }

    public static void test() {

        try {

            int numberOfControllers = Controllers.getControllerCount();
            for (int i = 0; i < numberOfControllers; i++) {
                Controller c = Controllers.getController(i);
                System.out.println("Controller " + c.getName());
                int buttons = c.getButtonCount();
                for (int b = 0; b < buttons; b++) {
                    System.out.println("Button " + b + " = " + c.getButtonName(b));
                }
                int selectedIndex = 0; // the index of the button we're checking

                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        // nothing to do
                    }
                    c.poll(); // update the controller from the hardware
                    if (c.isButtonPressed(selectedIndex)) {
                        // fire the huge gun!
                        System.out.println("fire");
                    }
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void test(int i) {
        Controller c = Controllers.getController(0);
        System.out.println("Controller " + c.getName());
        int buttons = c.getButtonCount();
        for (int b = 0; b < buttons; b++) {
            System.out.println("Button " + b + " = " + c.getButtonName(b));
        }
        int selectedIndex = 0; // the index of the button we're checking
        while (true) {
            c.poll(); // update the controller from the hardware
            if (c.isButtonPressed(selectedIndex)) {
                // fire the huge gun!
                System.out.println("fired index " + selectedIndex);
            } else {
                // System.out.println("not pressed");
            }

            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                // nothing to do
            }

        }
    }



    public static void test22() {
        ControllerEnvironment env = ControllerEnvironment.getDefaultEnvironment();
        Keyboard kb = null;
        for ( net.java.games.input.Controller c : env.getControllers()) {
            if (c instanceof Keyboard) {
                kb = (Keyboard) c;
            }
        }
        for (int i = 0; i < 100; i++) {
            kb.poll();
            if (kb.isKeyDown(Key.A)) {
                System.out.println("bingo!!!");
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                return;
            }
    }
    }



private static void printDetails(net.java.games.input.Controller c, PrintStream ps){
ps.println("Details for: " + c.getName() + ", " +
c.getType() + ", " + c.getPortType() );
printComponents(c.getComponents(), ps);
printRumblers(c.getRumblers(), ps);
// print details about any subcontrollers
net.java.games.input.Controller[] subCtrls = c.getControllers();
if (subCtrls.length == 0)
ps.println("No subcontrollers");
else {
ps.println("No. of subcontrollers: " + subCtrls.length);
// recursively visit each subcontroller
for (int i = 0; i < subCtrls.length; i++) {
ps.println("---------------");
ps.println("Subcontroller: " + i);
printDetails( subCtrls[i], ps);
}
}
} // end of printDetails()

private static void printComponents(
net.java.games.input.Component[] comps, PrintStream ps)
{ if (comps.length == 0)
ps.println("No Components");
else {
ps.println("Components: (" + comps.length + ")");
for (int i = 0; i < comps.length; i++)
ps.println( i + ". " +
comps[i].getName() + ", " +
getIdentifierName(comps[i]) + ", " +
(comps[i].isRelative() ? "relative" : "absolute") + ", " +
(comps[i].isAnalog() ? "analog" : "digital") + ", " +
comps[i].getDeadZone());
}
} // end of printComponents()

private static String getIdentifierName(net.java.games.input.Component comp)
{
net.java.games.input.Component.Identifier id = comp.getIdentifier();
if (id == net.java.games.input.Component.Identifier.Button.UNKNOWN)
return "button";
// an unknown button
else if(id == net.java.games.input.Component.Identifier.Key.UNKNOWN)
return "key";
// an unknown key
else
return id.getName();
}


private static void printRumblers(Rumbler[] rumblers,
PrintStream ps)
{ if (rumblers.length == 0)
ps.println("No Rumblers");
else {
ps.println("Rumblers: (" + rumblers.length + ")");
net.java.games.input.Component.Identifier rumblerID;
for (int i=0; i < rumblers.length; i++) {
rumblerID = rumblers[i].getAxisIdentifier();
ps.print(i + ". " + rumblers[i].getAxisName() +
" on axis; ");
if (rumblerID == null)
ps.println("no name");
else
ps.println("name: " + rumblerID.getName() );
}
}
} // end of printRumblers()

    private static PrintStream getPrintStream() {
        return System.out;
    }



} // end class

/*

Aggiungi alle opzioni di esecuzione:
-Djinput.plugins=net.java.games.input.DirectInputEnvironmentPlugin -Djinput.useDefaultPlugin=false
altrimenti è necessario che il programma abbia almeno una finestra attiva.

 */

